package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: yangwk
 * @date: 2023/1/18
 * @description:
 **/
public class P491NondecreasingSubsequences {
    public static void main(String[] args) {
        System.out.println(findSubsequences(new int[]{4, 6, 7, 7}));
    }

    public static List<List<Integer>> findSubsequences(int[] nums) {
        return findSubsequences(nums, 0, new ArrayList<>());
    }

    private static List<List<Integer>> findSubsequences(int[] nums, int start, List<Integer> list) {
        List<List<Integer>> result = new ArrayList<>();
        if (list.size() > 1) {
            result.add(new ArrayList<>(list));
        }
        for (int i = start; i < nums.length; i++) {
            if (!list.isEmpty() && list.get(list.size() - 1) > nums[i]) {
                continue;
            }
            if (hasDup(nums, start, i)) {
                continue;
            }
            list.add(nums[i]);
            result.addAll(findSubsequences(nums, i + 1, list));
            list.remove(list.size() - 1);
        }
        return result;
    }

    public static boolean hasDup(int[] arr, int start, int end) {
        for (int i = start; i < end; i++) {
            if (arr[i] == arr[end]) {
                return true;
            }
        }
        return false;
    }
}
